package com.yami.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.util.SendUtil;
import com.yami.shop.vo.group.UserIdVo;
import com.yami.shop.vo.group.whitelist.WhitelistVo;
import io.rong.RongCloud;
import io.rong.methods.group.Group;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.response.GroupBanWhitelistResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

import static com.yami.shop.common.response.ServerResponseEntity.fail;
import static com.yami.shop.common.response.ServerResponseEntity.success;

@Service
@Slf4j
public class RongYunWhiteListServiceImpl {
    @Autowired
    private RongCloud rongCloud;

    private Group group;

    @PostConstruct
    public void init() {
        this.group = rongCloud.group;
    }

    /**
     * 添加禁言白名单用户方法
     *
     * @param whitelistVo
     * @return
     */
    public ServerResponseEntity<String> add(WhitelistVo whitelistVo) {
        List<UserIdVo> groups = whitelistVo.getUsers();
        List<GroupMember> groupMembers = BeanUtil.copyToList(groups, GroupMember.class);
        GroupMember[] groupParam = groupMembers.toArray(new GroupMember[groupMembers.size()]);
        GroupModel param = new GroupModel().setId(whitelistVo.getGroupId()).setMembers(groupParam);
        try {
            Result result = group.ban.whitelist.user.add(param);
            log.info("group.ban.whitelist.add-result: {}", result);
            if (SendUtil.requestSuccess(result)) return success();
            return fail(ResponseEnum.EXCEPTION);
        } catch (Exception exception) {
            log.info("group.ban.whitelist.add-error: {}", exception.getMessage());
            return fail(ResponseEnum.EXCEPTION);
        }
    }

    /**
     * 查询禁言白名单用户方法
     *
     * @param groupId
     * @return
     */
    public ServerResponseEntity<GroupBanWhitelistResult> getList(String groupId) {
        try {
            GroupBanWhitelistResult result = (GroupBanWhitelistResult) group.ban.whitelist.user.getList(groupId);
            log.info("group.ban.whitelist.getList-result: {}", result);
            if (SendUtil.requestSuccess(result)) return success(result);
            return fail(ResponseEnum.EXCEPTION);
        } catch (Exception exception) {
            log.info("group.ban.whitelist.getList-error: {}", exception.getMessage());
            return fail(ResponseEnum.EXCEPTION);
        }
    }

    /**
     * 移除禁言白名单用户方法
     *
     * @param whitelistVo
     * @return
     */
    public ServerResponseEntity<String> remove(WhitelistVo whitelistVo) {
        List<UserIdVo> groups = whitelistVo.getUsers();
        List<GroupMember> groupMembers = BeanUtil.copyToList(groups, GroupMember.class);
        GroupMember[] groupParam = groupMembers.toArray(new GroupMember[groupMembers.size()]);
        GroupModel param = new GroupModel().setId(whitelistVo.getGroupId()).setMembers(groupParam);
        try {
            Result result = group.ban.whitelist.user.remove(param);
            log.info("group.ban.whitelist.remove-result: {}", result);
            if (SendUtil.requestSuccess(result)) return success();
            return fail(ResponseEnum.EXCEPTION);
        } catch (Exception exception) {
            log.info("group.ban.whitelist.remove-error: {}", exception.getMessage());
            return fail(ResponseEnum.EXCEPTION);
        }
    }
}
